package com.library.count.service;

import com.library.count.dao.CountDaoImpl;
import com.library.entity.Count;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly=false)
public class CountServiceImpl {
	
	@Resource
	private CountDaoImpl countDaoImpl;

	public void addCount(Count count){
		this.countDaoImpl.saveCount(count);
	}

    @Transactional(readOnly=true)
	public Count getCountByParams(Map paramsMap)
	{
		return this.countDaoImpl.findCountByProperty(paramsMap);
	}

	@Transactional(readOnly=true)
	public List<Count> getCountsByParams(Map paramsMap)
	{
		return this.countDaoImpl.findCountsByProperty(paramsMap);
	}


	/*统计总数*/
	@Transactional(readOnly=true)
	public Count getSumCountByParams(Map paramsMap)
	{
		List<Count> counts = this.countDaoImpl.findCountsByProperty(paramsMap);
		Count count = new Count();
		int sumStaffCount = 0;
		int sumBookCount = 0;
		int sumBorrowCount = 0;
		int sumEvaluateCount = 0;
		int sumSearchCount = 0;
		int sumPaymentCount = 0;

		for(int i = 0;i < counts.size();++i){
			sumStaffCount += counts.get(i).getStaffCount();
			sumBookCount += counts.get(i).getBookCount();
			sumBorrowCount += counts.get(i).getBorrowCount();
			sumEvaluateCount += counts.get(i).getEvaluateCount();
			sumSearchCount += counts.get(i).getSearchCount();
			sumPaymentCount += counts.get(i).getPaymentCount();
		}
		count.setStaffCount(sumStaffCount);
		count.setBookCount(sumBookCount);
		count.setBorrowCount(sumBorrowCount);
		count.setEvaluateCount(sumEvaluateCount);
		count.setSearchCount(sumSearchCount);
		count.setPaymentCount(sumPaymentCount);

		return count;
	}

	public void updateCount(Count count){
		this.countDaoImpl.updateCount(count);
	}

	/*删除指定评价记录*/
	public void deleteCount(String date){
		Count count = this.countDaoImpl.findByDate(date);
		this.countDaoImpl.deleteCount(count);
    }
}
